package com.yinuo.starter.util;

import com.yinuo.starter.properties.JmsProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
@Slf4j
public class JDBCUtil {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private JmsProperties jmsProperties;

    private final String IS_TABLE_EXIST_SQL = "select * from information_schema.tables where table_name = ? and table_schema = ?";

    public int createTable(String tableName, String sql){
        try {
            jdbcTemplate.update(sql);
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    public boolean isTableExist(String tableName) {
        try {
            Map<String, Object> map = jdbcTemplate.queryForMap(IS_TABLE_EXIST_SQL, tableName, jmsProperties.getDbName());
            if(map != null && map.size() > 0){
                return true;
            }
            return false;
        } catch (Exception e) {
            log.warn(tableName + "表不存在");
        }
        return false;
    }

    public JdbcTemplate getJdbcTemplate(){
        return jdbcTemplate;
    }
}
